<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="500px"
    @close="close"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="角色名称" prop="roleName">
        <el-input v-model="form.roleName" autocomplete="off"></el-input>
      </el-form-item>
      <!-- <el-form-item label="菜单" prop="menu">
        <el-tree
          ref="tree"
          :data="data"
          show-checkbox
          node-key="id"
          :default-checked-keys="[2]"
        ></el-tree>
      </el-form-item> -->
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { addOrUpdateAdminRole } from '@/api/roleManagement'
  import { getAllMenu } from '@/api/menuManagement'
  import _ from 'lodash'

  export default {
    name: 'RoleManagementEdit',
    data() {
      return {
        data: '',
        form: {
          roleName: '',
          // menuIds: '',
        },
        rules: {
          roleName: [
            { required: true, trigger: 'blur', message: '请输入角色名称' },
          ],
        },
        title: '',
        dialogFormVisible: false,
      }
    },
    created() {},
    methods: {
      async initTree() {
        const { data } = await getAllMenu()
        console.log(data)
        if (data.status == 0) {
          let AllData = _.cloneDeep(data.body)
          for (
            let index = 0, length = AllData.length;
            index < length;
            index++
          ) {
            const element = AllData[index]
            element.label = element.meta.title
            if (element.children !== null) {
              console.log(element.children)
              for (let i = 0, len = element.children.length; i < len; i++) {
                const e = element.children[i]
                e.label = e.meta.title
              }
            }
          }
          this.data = AllData
        } else {
          this.$baseMessage(data.message, 'error')
        }
      },
      showEdit(row) {
        if (!row) {
          this.title = '添加'
        } else {
          this.title = '编辑'
          this.form = Object.assign({}, row)
        }
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            const { data } = await addOrUpdateAdminRole(this.form)
            if (data.status === 0) {
              this.$baseMessage(data.message, 'success')
              this.$emit('fetch-data')
              this.close()
            } else {
              this.$baseMessage(data.message, 'error')
            }
          } else {
            return false
          }
        })
      },
    },
  }
</script>

<style lang="scss" scoped>
  // .menuStyle {
  //   border: 1px solid #dddee6;
  //   height: 400px;
  // }
</style>
